home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / fileobj.com / FILEOBJ.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-07-11  |  6.8 KB  |  247 lines

  1. Unit FileObj;
  2.  
  3. {$V-}
  4. {$R-}
  5. {$S-}
  6.  
  7. Interface
  8.  
  9. {
  10.  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11.   FILEOBJ.PAS
  12.  
  13.     This unit contains the object FileObjType.  This object collects and makes
  14.     available information about a given file.  It is a handy way to check
  15.     various features of a file without a lot of coding.
  16.  
  17.     To use this object, either define a variable of the type FileObjType, or
  18.     create a dynamic object with a call to the New procedure/function.
  19.     Initialize the file with the name of the file of interest.  Among other
  20.     things, the object can tell you
  21.  
  22.             - if the file exists
  23.             - the fully qualified file name
  24.             - the file base name
  25.             - the file date, time and attributes
  26.             - the file size
  27.  
  28.     Note that a given instance of this object can be used over and over
  29.     within the same program.  Simply reinitialize the unit with a new
  30.     file name.
  31.  
  32.     I wrote this to try out OOP.  I find it especially useful for
  33.     command line parameter checking in my programs.
  34.  
  35.     Bill Matsoukas [ 76416,3622 ]
  36.  
  37.  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38.  }
  39.  
  40. Uses Dos;
  41.  
  42. Type
  43.  
  44.   DriveStr = String[2]; { should this be standard in the DOS unit ?!? }
  45.   DateStr = String[10];
  46.   TimeStr = String[8];
  47.   AttrStr = String[5];
  48.  
  49.   FileObjPtr = ^FileObjType;  { for dynamically allocated objects }
  50.  
  51.   FileObjType = Object
  52.                           { object data }
  53.     FPath     : PathStr;
  54.     FDrive    : DriveStr;
  55.     FBaseName : NameStr;
  56.     FExt      : ExtStr;
  57.     FDir      : DirStr;
  58.     FLength   : LongInt;
  59.     FExists   : Boolean;
  60.     FDate     : DateStr;
  61.     FTime     : TimeStr;
  62.     FAttrStr  : AttrStr;
  63.     FAttr     : Word;
  64.                           { object methods }
  65.  
  66.     Constructor Init ( FileName : PathStr ); { init with fully or partially
  67.                                                quallified file name            }
  68.     Destructor Done; Virtual;                { empty function for deallocation }
  69.     Function GetPath : PathStr;              { returns fully qualified name    }
  70.     Function GetDrive : DriveStr;            { returns drive letter and colon  }
  71.     Function GetBaseName : NameStr;          { returns stripped file name      }
  72.     Function GetExt : ExtStr;                { returns dot plus extension      }
  73.     Function GetDir : DirStr;                { returns path to file            }
  74.     Function GetLength : LongInt;            { returns file length in bytes    }
  75.     Function FGetDate : DateStr;             { returns file date in string
  76.                                                form "YYYY-MM-DD"               }
  77.     Function FGetTime : TimeStr;             { returns the file time in string
  78.                                                form "HH:MM:SS"                 }
  79.     Function FGetAttr : Word;                { returns file attribute word     }
  80.     Function FGetAttrStr : AttrStr;          { returns the file attribute in
  81.                                                unix-like string "RHSAX" where
  82.                                                   R = read only
  83.                                                   H = hidden
  84.                                                   S = system
  85.                                                   A = archive
  86.                                                   X = executable
  87.                                                a dash ("-") character replaces
  88.                                                false attributes.               }
  89.     Function FileExists : Boolean;           { true if file exists, else false }
  90.  
  91.   End;
  92.  
  93. Implementation
  94.  
  95. { *******  private routines used by FileObjType methods  ******* }
  96.  
  97. Procedure SubZeros ( Var S : String );
  98. Var
  99.   I : Byte absolute S;
  100.   J : Byte;
  101. Begin
  102.   If I = 0 then
  103.     Exit;
  104.   For J := 1 to I do
  105.     If S[J] = ' ' then
  106.       S[J] := '0';
  107. End;
  108.  
  109. Function StrObjDate ( Yr, Mo, Da : Word ) : DateStr;
  110. Var
  111.   YrStr : String[4];
  112.   MoStr : String[2];
  113.   DaStr : String[2];
  114.   DtStr : DateStr;
  115. Begin
  116.   Str ( Yr:4, YrStr );
  117.   Str ( Mo:2, MoStr );
  118.   Str ( Da:2, DaStr );
  119.   DtStr := YrStr + '-' + MoStr + '-' + DaStr;
  120.   SubZeros ( DtStr );
  121.   StrObjDate := DtStr;
  122. End;
  123.  
  124. Function StrObjTime ( Hr, Mi, Se : Word ) : TimeStr;
  125. Var
  126.   HrStr : String[2];
  127.   MiStr : String[2];
  128.   SeStr : String[2];
  129.   TmStr : TimeStr;
  130. Begin
  131.   Str ( Hr:4, HrStr );
  132.   Str ( Mi:2, MiStr );
  133.   Str ( Se:2, SeStr );
  134.   TmStr := HrStr + ':' + MiStr + ':' + SeStr;
  135.   SubZeros ( TmStr );
  136.   StrObjTime := TmStr;
  137. End;
  138.  
  139. Function StrObjAttr ( Attr : Word; Ext : ExtStr ) : AttrStr;
  140. Var
  141.   S : AttrStr;
  142. Begin
  143.   S := '-----';
  144.   If ( ( Attr and ReadOnly ) = ReadOnly ) then S[1] := 'R';
  145.   If ( ( Attr and Hidden ) = Hidden ) then S[2] := 'H';
  146.   If ( ( Attr and SysFile ) = SysFile ) then S[3] := 'S';
  147.   If ( ( Attr and Archive ) = Archive ) then S[4] := 'A';
  148.   If ( Ext = '.COM' ) or
  149.      ( Ext = '.EXE' ) or
  150.      ( Ext = '.BAT' ) then S[5] := 'X';
  151.   StrObjAttr := S;
  152. End;
  153.  
  154. { *******  FileObjType methods  ********* }
  155.  
  156. Constructor FileObjType.Init ( FileName : PathStr );
  157. Var
  158.   FRec : SearchRec;
  159.   DRec : DateTime;
  160. Begin
  161.   FPath := FExpand ( FileName );
  162.   FSplit ( FPath, FDir, FBaseName, FExt );
  163.   FDrive := Copy ( FDir, 1, 2 );
  164.   FDir := Copy ( FDir, 3, Length ( FDir ) - 2 );
  165.   FindFirst ( FPath, AnyFile Xor ( Directory Or VolumeId ), FRec );
  166.   FExists := ( DosError = 0 );
  167.   If FExists then
  168.   Begin
  169.     FLength := FRec.Size;
  170.     UnPackTime ( FRec.Time, DRec );
  171.     FDate := StrObjDate ( DRec.Year, DRec.Month, DRec.Day );
  172.     FTime := StrObjTime ( DRec.Hour, DRec.Min, DRec.Sec );
  173.     FAttr := FRec.Attr;
  174.     FAttrStr := StrObjAttr ( FAttr, FExt );
  175.   End
  176.   Else
  177.   Begin
  178.     Flength := 0;
  179.     FAttr := 0;
  180.     FillChar ( FDate, SizeOf ( FDate ), 0 );
  181.     FillChar ( FTime, SizeOf ( FTime ), 0 );
  182.     FillChar ( FAttrStr, SizeOf ( FAttrStr ), 0 );
  183.   End;
  184. End;
  185.  
  186. Destructor FileObjType.Done;
  187. Begin
  188. End;
  189.  
  190. Function FileObjType.GetPath : PathStr;
  191. Begin
  192.   GetPath := FPath;
  193. End;
  194.  
  195. Function FileObjType.GetDrive : DriveStr;
  196. Begin
  197.   GetDrive := FDrive;
  198. End;
  199.  
  200. Function FileObjType.GetBaseName : NameStr;
  201. Begin
  202.   GetBaseName := FBaseName;
  203. End;
  204.  
  205. Function FileObjType.GetExt : ExtStr;
  206. Begin
  207.   GetExt := FExt;
  208. End;
  209.  
  210. Function FileObjType.GetDir : DirStr;
  211. Begin
  212.   GetDir := FDir;
  213. End;
  214.  
  215. Function FileObjType.GetLength : LongInt;
  216. Begin
  217.   GetLength := FLength;
  218. End;
  219.  
  220. Function FileObjType.FGetDate : DateStr;
  221. Begin
  222.   FGetDate := FDate;
  223. End;
  224.  
  225. Function FileObjType.FGetTime : TimeStr;
  226. Begin
  227.   FGetTime := FTime;
  228. End;
  229.  
  230. Function FileObjType.FGetAttr : Word;
  231. Begin
  232.   FGetAttr := FAttr;
  233. End;
  234.  
  235. Function FileObjType.FGetAttrStr : AttrStr;
  236. Begin
  237.   FGetAttrStr := FAttrStr;
  238. End;
  239.  
  240. Function FileObjType.FileExists : Boolean;
  241. Begin
  242.   FileExists := FExists;
  243. End;
  244.  
  245. End.
  246.  
  247.